﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Threading;
using System;

namespace JPGResizer.Pages
{
    /// <summary>
    /// Interaction logic for Page4.xaml
    /// </summary>
    public partial class Page4 : Page
    {
        public Page4()
        {
            InitializeComponent();
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            this.CurrentProgress.Value = this.CurrentProgress.Minimum;
            Helper.ValueHelper.CurrentValues.SaveImagesProgressChanged = SaveImageProgressChanged;
            this.Start.IsEnabled = false;
            //------------------------------------------------------------
            foreach (var img in Helper.ValueHelper.CurrentValues.Images)
            {
                ScaleTransform scare = null;
                if (img != null && !string.IsNullOrEmpty(img.Filename) && img.ImageSource != null)
                {
                    switch (Helper.ValueHelper.CurrentValues.ResizeType)
                    {
                        case Helper.ResizeType.ResizeType1:
                            {
                                double percent = (double)Helper.ValueHelper.CurrentValues.Length / (double)img.ImageSource.PixelWidth;
                                scare = new ScaleTransform(percent, percent);
                                break;
                            }
                        case Helper.ResizeType.ResizeType2:
                            {
                                double percent =
                                    (double)Helper.ValueHelper.CurrentValues.Length / (double)(img.ImageSource.PixelWidth > img.ImageSource.PixelHeight ? img.ImageSource.PixelWidth : img.ImageSource.PixelHeight);
                                scare = new ScaleTransform(percent, percent);
                                break;
                            }
                        case Helper.ResizeType.ResizeType3:
                            {
                                scare = new ScaleTransform(Helper.ValueHelper.CurrentValues.ResizePercent / 100,
                                                           Helper.ValueHelper.CurrentValues.ResizePercent / 100);
                                break;
                            }
                        case Helper.ResizeType.ResizeType4:
                            {
                                break;
                            }
                    }
                    Helper.ValueHelper.CurrentValues.Scale_SaveImage(img,scare);
                }
            }
            //------------------------------------------------------------
            this.txtResult.Visibility = System.Windows.Visibility.Visible;
        }

        private void SaveImageProgressChanged(double value)
        {
            this.CurrentProgress.Dispatcher.Invoke(
                new Action<System.Windows.DependencyProperty, object>(this.CurrentProgress.SetValue), 
                System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value);
        }
    }
}
